// Copyright (C) 2024 wlly-lzh
// Created On 24/3/6
// 公用栈
#ifndef _LINKEDSTACK_C
#define _LINKEDSTACK_C

#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include "linkedlist.c"

typedef struct LinkedStack LinkedStack;
struct LinkedStack {
	struct LinkedList * List;
	int Length;
};

LinkedStack * Create_LinkedStack(){
	LinkedStack * newStack;
	newStack =(LinkedStack *)malloc(sizeof(LinkedStack));
	if(newStack == NULL){
		printf("liblds: Malloc False!\n");
		exit(3);
	}
	newStack->Length=0;
	newStack->List=NULL;
	return newStack;
}
void Destroy_LinkedStack(LinkedStack **Stack){
	if((*Stack)->List != NULL)
		Clear_LinkedList((*Stack)->List);
	free(*Stack);
	*Stack=NULL;
}
void Push_LinkedStack(LinkedStack *Stack,void *Data){
	LinkedList *newList=(LinkedList *)malloc(sizeof(LinkedList));
	if(newList == NULL){
		printf("liblds: Malloc False!\n");
		exit(3);
	}
	newList->Data=Data;
	newList->Next=Stack->List;
	Stack->List=newList;
	Stack->Length++;
}
void Pop_LinkedStack(LinkedStack *Stack,void **Data){
	LinkedList *Del=Stack->List;
	if(Stack->Length > 0){
		*Data=Stack->List->Data;
		Stack->List=Stack->List->Next;
		free(Del);
		Stack->Length--;
	}else{
		printf("liblds: Stack empty\n");
	}
}
void GetTop_LinkedStack(const LinkedStack *Stack,void **Data){
	if(Stack->Length > 0){
		*Data=Stack->List->Data;
	}
}
bool IsEmpty_LinkedStack(const LinkedStack * Stack){
	if(Stack->Length == 0)
		return true;
	else
		return false;
}

#endif // _LINKEDSTACK_C
